package com.learning.jquizenglish.common.adapter;

import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.learning.jquizenglish.model.PageItemInfo;

public class JQuizViewPagerAdapter extends FragmentStatePagerAdapter{

private ArrayList<PageItemInfo> mListPageInfo;
	
	public JQuizViewPagerAdapter(FragmentManager fm,ArrayList<PageItemInfo> listPageInfo) {
        super(fm);
        mListPageInfo = listPageInfo;
        if (mListPageInfo == null) {
			mListPageInfo = new ArrayList<PageItemInfo>();
		}
    }

    /* (non-Javadoc)
	 * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
	 */
	@Override
    public Fragment getItem(int position) {
    	Fragment frag = null;
		try {
			frag = (Fragment) mListPageInfo.get(position).fragClass.newInstance();
		} catch (java.lang.InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
        return frag;
    }

    /* (non-Javadoc)
	 * @see android.support.v4.view.PagerAdapter#getCount()
	 */
	@Override
    public int getCount() {
        return mListPageInfo.size();
    }

    /* (non-Javadoc)
	 * @see android.support.v4.view.PagerAdapter#getPageTitle(int)
	 */
	@Override
    public CharSequence getPageTitle(int position) {
		if (mListPageInfo.get(position).pageTitle!=null) {
			return mListPageInfo.get(position).pageTitle;
	 	}else if (mListPageInfo.get(position).spannableStringBuilder != null) {
			return mListPageInfo.get(position).spannableStringBuilder;
		}else{
	 		return "";
	 	}
    }

}
